package com.bnuz.vtl.controller;

import com.bnuz.vtl.model.PrivateNode;
import com.bnuz.vtl.model.PublicNode;
import com.bnuz.vtl.model.User;
import com.bnuz.vtl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

@RequestMapping("/api/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("")
    public User saveUser(@RequestBody User user) throws Exception{
        return userService.save(user);
    }

    @GetMapping("/info")
    public Principal getCurrentUserPrincipal(Principal user) throws Exception{
        return user;
    }

    @GetMapping("/follow/{id}")
    public void followSubject(Principal user, @PathVariable("id") String subjectId){
        userService.followSubject(user.getName(), subjectId);
    }

//    @PostMapping("/")
//    public void deleteByUser(@RequestBody User user) throws Exception{
//         userService.deleteByEntity(user);
//    }

    // {}里面的东西要和参表里面的一样，或者手动指定
    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable("id") String userId) throws Exception{
        userService.deleteById(userId);
    }

    @GetMapping("")
    public List<User> findUserAll() throws Exception{
        return userService.findAll();
    }

    @GetMapping("/{id}")
    public User findUserById(@PathVariable("id") String userId) throws Exception{
        return userService.findById(userId);
    }

    @GetMapping("/~{username}")
    public User findUserByUsername(@PathVariable("username") String username) throws Exception{
        return userService.findByUsername(username);
    }


    @GetMapping("/~{username}/tree")
    public PublicNode getUserSubjectTree(@PathVariable("username") String username){
        return userService.getUserSubjectTree(username);
    }

    @GetMapping("/~{username}/nodes")
    public List<PrivateNode> getUserNodes(@PathVariable("username") String username) {
        return userService.getUserNodes(username);
    }

}
